package cn.jnetty.ioc.configruation;

import cn.jnetty.api.exception.JnettyIocException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.util.*;

/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/4/3 16:28
 * 描述：
 */
public class YmalConfiguration {
    private final Logger log = LoggerFactory.getLogger(YmalConfiguration.class);
    private  LinkedHashMap<String,Object> ymls= new LinkedHashMap<>();
    void loading(String file){
        try {
            LinkedHashMap linkedHashMap = new Yaml().loadAs(this.getClass().getResourceAsStream("/" + file), LinkedHashMap.class);
            if(linkedHashMap == null){
                return;
            }
            ymls.putAll(linkedHashMap);
            log.debug("解析的yml文件注解字段=======" + ymls);
        }catch (Exception e){
            throw new JnettyIocException("加载配置文件失败，找不到{0}这个文件",e,file);
        }
    }
    public String getValue(String key){
        String[] strs = key.split("\\.");
        int i = 0;
        Object o =  ymls.get(strs[0]);
        while (o != null && o.getClass() == LinkedHashMap.class){
            i++;
            String k = strs[i];
            o = ((LinkedHashMap)o).get(k);
        }
        if(o == null){
            return null;
        }
        return o.toString();
    }
}
